<?php
/**
 * $Id: MysqlConnection.class.php 264 2009-03-22 06:29:51Z coretxt $
 * @package repository
 * @subpackage store.mysql
 *
 * Copyright (c) 2007-2009 Coretxt
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
require_once dirname(__FILE__) .'/../../../framework/EventLog.class.php';
require_once dirname(__FILE__) .'/../ResourceError.class.php';

/**
 * An active connection to a MySql database server.
 *
 * @package repository
 * @subpackage store.mysql
 */
class MysqlConnection {

	private $_connection;
	private $_db_host;
	private $_db_name;
	private $_db_user;
	private $_db_pass;

	/**
	 *  Constructs a Mysql connection that can be obtained upon invocation of the connect method.
	 *
	 * @param $host Address of database server
	 * @param $name Name of database to connect to
	 * @param $user Authorized user name on database
	 * @param $pass Authorized password for user
	 */
	 function __construct($environment = false) {
		 if ($environment) {
			$this->_db_host = $environment->DB_HOST;
			$this->_db_name = $environment->DB_NAME;
			$this->_db_user = $environment->DB_USER;
			$this->_db_pass = $environment->DB_PASS;
		 } else {
			$this->_db_host = DB_HOST;
			$this->_db_name = DB_NAME;
			$this->_db_user = DB_USER;
			$this->_db_pass = DB_PASS;
		 }
	 }
	
	/**
	 * Idempotent method creates the low level connection to the database.
	 *
	 * @public
	 * @return boolean true on success
	 */
	function connect() {
		if (!is_resource($this->_connection)) {
			$this->_connection = @mysql_connect($this->_db_host, $this->_db_user, $this->_db_pass);
			if (!is_resource($this->_connection)) {
				$this->raiseError();
				return false;
			}
			EventLog::info("Connected to [{$this->_db_host}]");
			if (!@mysql_select_db($this->_db_name, $this->_connection)) {
				$this->raiseError();
				return false;
			}
			EventLog::info("Selected [{$this->_db_name}]");
		}
		return true;
	}

	/**
	 * @private
	 */
	function raiseError() {
		$message = mysql_error();
		throw new ResourceError($message);
	}
	
	/**
	 * Issue an SQL query against the database
	 */
	function execute($sql) {
		$this->connect();
		$query = mysql_query($sql, $this->_connection);
		EventLog::info("Executed [$sql]");
		return (mysql_error() != '') ? $this->raiseError() : $query;
	}
	
}

?>